package com.yj.provider.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * 使用base64实现加密解密
 */
public class Base64Util {

    private static Base64.Encoder base64Encoder = Base64.getEncoder();
    private static Base64.Decoder base64Decoder = Base64.getDecoder();

    public static void main(String[] args) throws UnsupportedEncodingException {
        //加密
        String h1 = encode("hello world");
        System.out.println(h1);
        //解密
        String decode = decode(h1);
        System.out.println(decode);
    }

    public static String encode(String txt) throws UnsupportedEncodingException {
        String encode = base64Encoder.encodeToString(txt.getBytes("UTF-8"));

        return encode;
    }

    public static String decode(String txt) throws UnsupportedEncodingException {
        byte[] decode = base64Decoder.decode(txt);
        String s = new String(decode, "UTF-8");
        return s;
    }



}
